home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TEXTEDIT.SWG / 0007_WORDWRP3.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  86 lines

  1. Var
  2.   S : String;
  3.  
  4. Function Wrap(Var st: String; maxlen: Byte; justify: Boolean): String;
  5.   { returns a String of no more than maxlen Characters With the last   }
  6.   { Character being the last space beFore maxlen. On return st now has }
  7.   { the remaining Characters left after the wrapping.                  }
  8.   Const
  9.     space = #32;
  10.   Var
  11.     len      : Byte Absolute st;
  12.     x,
  13.     oldlen,
  14.     newlen   : Byte;
  15.  
  16.   Function JustifiedStr(s: String; max: Byte): String;
  17.  
  18.     { Justifies String s left and right to length max. if there is more }
  19.     { than one trailing space, only the right most space is deleted. The}
  20.     { remaining spaces are considered "hard".  #255 is used as the Char }
  21.     { used For padding purposes. This will enable easy removal in any   }
  22.     { editor routine.                                                   }
  23.  
  24.     Const
  25.       softSpace = #255;
  26.     Var
  27.       jstr      : String;
  28.       len       : Byte Absolute jstr;
  29.     begin
  30.       jstr := s;
  31.       While (jstr[1] = space) and (len > 0) do   { delete all leading spaces }
  32.         delete(jstr,1,1);
  33.       if jstr[len] = space then
  34.         dec(len);                                { Get rid of trailing space }
  35.       if not ((len = max) or (len = 0)) then begin
  36.         x := pos('.',jstr);     { Attempt to start padding at sentence break }
  37.         if (x = 0) or (x =len) then       { no period or period is at length }
  38.           x := 1;                                    { so start at beginning }
  39.         if pos(space,jstr) <> 0 then Repeat        { ensure at least 1 space }
  40.           if jstr[x] = space then                      { so add a soft space }
  41.             insert(softSpace,jstr,x+1);
  42.           x := succ(x mod len);  { if eoln is reached return and do it again }
  43.         Until len = max;        { Until the wanted String length is achieved }
  44.       end; { if not ... }
  45.       JustifiedStr := jstr;
  46.     end; { JustifiedStr }
  47.  
  48.  
  49.   begin  { Wrap }
  50.     if len <= maxlen then begin                       { no wrapping required }
  51.       Wrap := st;
  52.       len  := 0;
  53.     end else begin
  54.       oldlen := len;                { save the length of the original String }
  55.       len    := succ(maxlen);                        { set length to maximum }
  56.       Repeat                     { find last space in st beFore or at maxlen }
  57.         dec(len);
  58.       Until (st[len] = space) or (len = 0);
  59.       if len = 0 then                   { no spaces in st, so chop at maxlen }
  60.         len := maxlen;
  61.       if justify then
  62.         Wrap := JustifiedStr(st,maxlen)
  63.       else
  64.         Wrap := st;
  65.       newlen :=  len;          { save the length of the newly wrapped String }
  66.       len := oldlen;              { and restore it to original length beFore }
  67.       Delete(st,1,newlen);              { getting rid of the wrapped portion }
  68.     end;
  69.   end; { Wrap }
  70.  
  71. begin
  72.   S :=
  73. 'By Far the easiest way to manage a database is to create an '+
  74. 'index File. An index File can take many Forms and its size will depend '+
  75. 'upon how many Records you want in the db. The routines that follow '+
  76. 'assume no more than 32760 Records.';
  77.  
  78. While length(S) <> 0 do
  79.   Writeln(Wrap(S,60,True));
  80. end.
  81.  
  82. Whilst this is tested and known to work on the example String, no further
  83. testing than that has been done.  I suggest you test it a great deal more
  84. beFore being satisfied that it is OK.
  85.  
  86.